package com.onlineshop.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	
	public static final SimpleDateFormat FORMAT_YMD = new SimpleDateFormat("yyyy-MM-dd");
	
	public static final SimpleDateFormat FORMAT_YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static String formatDateToStringYMDHMS(Date date) {
	    if (date == null) {
	        return "";
	    }
	    try {
	        return FORMAT_YMD_HMS.format(date);
	    } catch(Exception e) {
	        Log.run.warn("Date change to YMD_HMS string fail! with date=" + date);
	    }
	    return "";
	}
	
	public static Date parseStringToDateYMDHMS(String str) {
		if (str!=null&str.length()>0) {
			try {
				return parseStringToDate(str, FORMAT_YMD_HMS);
			} catch (Exception e) {
				Log.run.warn("String parse to Date YMD_HMS fail! with str=" + str);
			}
		}
		return null;
	}
	
	public static String formatDateToStringYMD(Date date) throws Exception{
		return FORMAT_YMD.format(date);
	}
	
	public static Date parseStringToDateYMD(String str) throws Exception{
		return FORMAT_YMD.parse(str);
	}
	
	public static String formatDateToString(Date date, SimpleDateFormat format) throws Exception{
	    if (date == null) {
	        return "";
	    }
		return format.format(date);
	}
	
	public static Date parseStringToDate(String str, SimpleDateFormat format) throws Exception{
		return format.parse(str);
	}

}
